//
//  Meal.swift
//  TestSwift3
//
//  Created by ficow on 16/8/16.
//  Copyright © 2016年 ficow. All rights reserved.
//

import UIKit

class Meal:NSObject,NSCoding{
    
    // MARK: PROPERTIES
    var name: String
    var photo:UIImage?
    var rating:Int
    
    // MARK:ARCHIVING PATHS
    static let DocumentsDirectory = FileManager().urls(for: .documentDirectory, in: .userDomainMask).first!
    static let ArchiveURL = DocumentsDirectory.appendingPathComponent("meals")
    
    
    // MARK: TYPE
    struct PropertyKey{
        
        static let nameKey = "name"
        static let photoKey = "photo"
        static let ratingKey = "rating"
    }
    
    
    // MARK: INIT
    init?(name:String,photo:UIImage?,rating:Int) {
        
        self.name = name
        self.photo = photo
        self.rating = rating
        
        super.init()
        
        if name.isEmpty || rating < 0 {
            return nil
        }
    }
    
    // MARK: CODING 
    func encode(with aCoder: NSCoder) {
        
        aCoder.encode(name, forKey: PropertyKey.nameKey)
        aCoder.encode(photo, forKey: PropertyKey.photoKey)
        aCoder.encode(rating, forKey: PropertyKey.ratingKey)
        
    }
    required convenience init?(coder aDecoder: NSCoder) {
        
        let name = aDecoder.decodeObject(forKey: PropertyKey.nameKey) as! String
        let photo = aDecoder.decodeObject(forKey: PropertyKey.photoKey) as? UIImage
        let rating = aDecoder.decodeInteger(forKey: PropertyKey.ratingKey)
        
        self.init(name:name,photo:photo,rating:rating)
    }
}
